package cn.bdqn.service.order;

import cn.bdqn.dao.order.OrderStateMapper;
import cn.bdqn.pojo.order.OrderState;
import cn.bdqn.utils.Json;
import cn.bdqn.utils.RedisCache;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static cn.bdqn.utils.RedisConstants.ORDER_STATE_KEY;

@Service
public class OrderStateService extends ServiceImpl<OrderStateMapper, OrderState> {

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询并存放redis 订单状态
     * @return
     */
    public Json getOrderState() {
        List<OrderState> cacheObject = redisCache.getCacheObject(ORDER_STATE_KEY);
        if (Objects.nonNull(cacheObject)){
            return Json.ok(cacheObject);
        }
        List<OrderState> list = query().list();
        if (Objects.isNull(list)){
            return Json.fall("查询失败");
        }
        redisCache.setCacheObject(ORDER_STATE_KEY,list);
        return Json.ok(list);
    }
}
